package com.lphl.pet.util;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.lphl.pet.R;

public class PopWindowUtils implements OnClickListener {

    private Context ct;
    private View parent;
    private View view;
    private PopupWindow popupWindow;
    private RelativeLayout rl_trade_edition;
    private RelativeLayout rl_flagship_version;
    private LinearLayout ll_add_new_accessories;

    public PopWindowUtils(Context ct, View parent) {
        this.ct = ct;
        this.parent = parent;
    }

//	private static PopWindowUtils pop = null;
//	private PopWindowUtils (){}
//	public static PopWindowUtils getInstance(){
//		if(pop == null){
//			pop = new PopWindowUtils();
//		}
//		return pop;
//	}

    /**
     * 显示
     *
     */
    public void showWindow() {

        if (popupWindow == null) {
            LayoutInflater layoutInflater = (LayoutInflater) ct.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.activity_custom_popwindow_group_list, null);
            initView();

            // 创建一个PopuWidow对象
            popupWindow = new PopupWindow(view, 500, 650);
        }

        // 使其聚集
        popupWindow.setFocusable(true);
        // 设置允许在外点击消失
        popupWindow.setOutsideTouchable(true);

        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        WindowManager windowManager = (WindowManager) ct.getSystemService(Context.WINDOW_SERVICE);
        // 显示的位置为:屏幕的宽度的一半-PopupWindow的高度的一半
        int xPos = windowManager.getDefaultDisplay().getWidth() / 2 - popupWindow.getWidth() / 2;

        popupWindow.showAsDropDown(parent, xPos, 0);

    }

    private void initView() {
        rl_trade_edition = (RelativeLayout) view.findViewById(R.id.rl_trade_edition);
        rl_flagship_version = (RelativeLayout) view.findViewById(R.id.rl_flagship_version);
        ll_add_new_accessories = (LinearLayout) view.findViewById(R.id.ll_add_new_accessories);
        rl_trade_edition.setOnClickListener(this);
        rl_flagship_version.setOnClickListener(this);
        ll_add_new_accessories.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_trade_edition:

                break;

            case R.id.rl_flagship_version:

                break;

            case R.id.ll_add_new_accessories:
                Toast.makeText(ct, "添加一个新的", 0).show();

                break;

            default:
                break;
        }
    }
}
